<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :xl="6" :lg="7" :md="8" :sm="9" :xs="10">
      <h2>欢迎来到时政热点管理系统</h2>
      <el-image lazy src="http://guangyuanol.cn/uploads/allimg/200227/19210J4O-4.jpg" style="width: 180px; height: 180px;"></el-image>
      <p>运维QQ 1548940268</p>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical" content-position="center"></el-divider>
    </el-col>
    <el-col :xl="6" :lg="7" :md="8" :sm="9" :xs="10">
      <el-form :model="loginModel" :rules="rules" ref="loginModel" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username" style="width: 380px">
          <el-input v-model="loginModel.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="width: 380px">
          <el-input type="password" v-model="loginModel.password"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code" style="width: 380px">
          <el-input v-model="loginModel.code" style="width: 150px; float: left"></el-input>
          <el-image class="captcha" :src="captchaBaseCode" @click="getCaptchaBaseCode"></el-image>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginModel')">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>

  import qs from 'qs';

export default {
  name: "Login",
  data() {
    return {
      loginModel: {
        username: '',
        password: '',
        code: '',
        token: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 6, max: 24, message: '用户名长度在 6 - 24 个字符之间', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, message: '用户名最小需要 6 位', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 5, max: 5, message: '验证码长度为 5 位', trigger: 'blur'},
        ],
      },
      captchaBaseCode: ''
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post('/login?' + qs.stringify(this.loginModel)).then((res) => {

            // 获取JWT
            const jwtCode = res.headers['authorization']
            // 状态管理
            this.$store.commit('setToken', jwtCode)

            this.$router.push('/admin/index')

          }).catch(error => {
            this.getCaptchaBaseCode()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    getCaptchaBaseCode() {
      this.$http.get('/captcha').then((res) => {
        console.log(res.data)
        this.loginModel.token = res.data.data.token
        this.captchaBaseCode = res.data.data.captchaBaseCode
      })
    }
  },
  created() {
    this.getCaptchaBaseCode()
  }
}
</script>

<style lang="less" scoped>

.el-row {
  background-color: #FAFAFA;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center; // 交叉轴居中
  text-align: center;

  .el-divider {
    height: 200px
  }

  .captcha {
    float: left;
    margin-left: 8px;
  }
  .captcha:hover {
    cursor: pointer;
  }
}

</style>
